package com.unittec.zk.provider.auth.controller;

import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unittec.zk.provider.auth.excel.imports.ExcelImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.StringUtil;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.auth.domain.ImportTaskDto;
import com.unittec.zk.sdk.auth.request.Req101921;
import com.unittec.zk.sdk.auth.request.Req101922;
import com.unittec.zk.sdk.auth.request.Req101923;
import com.unittec.zk.sdk.auth.response.Res101921;
import com.unittec.zk.sdk.auth.response.Res101922;
import com.unittec.zk.sdk.auth.response.Res101923;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description:
 * @Author: wuxinwei
 * @CreateDate: 2019/11/18 11:09
 */
@RestController
@Api(tags = "EXCEL导入管理")
public class ExcelImportController {

    @Autowired
    private Log log;
    @Autowired
    private ExcelImportService excelImportService;

    @RequestMapping(value = "/101921", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "EXCEL导入", notes = "EXCEL导入")
    public Result<Res101921> importExcel(@RequestBody GatewayRequest<Req101921> req) {
        log.debug(req, "importExcel  {},param:{}  ", "begin", JSON.toJSONString(req.getContent()));
        long startLogTime = System.currentTimeMillis();
        try {
            // 参数校验
            ValidationUtils.validateEntityStr(req.getContent());
        } catch (Exception e) {
            log.error(req, "importExcel  error  end ", e);
            log.addOperateLog("101921", JSON.toJSONString(req.getContent()), "EXCEL导入", "参数错误：" + log.buildErrmsg(e),
                LogOperateEnum.LOG_IMPORT.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, BizCode.PARAM_ERROR.getCode(), e.getMessage());
        }
        String consumer = req.getContent().getConsumer();
        String key = req.getContent().getFileKey();
        String path = req.getContent().getFilePath();
        String fileName = req.getContent().getFileName();
        String type = req.getContent().getType();

        Map<String, Object> paramMap =
            req.getContent().getParamMap() == null ? new HashMap<>(16) : req.getContent().getParamMap();
        boolean async = req.getContent().getAsync() == null ? false : req.getContent().getAsync();

        try {
            Object obj = excelImportService.importExcel(consumer, key,path, fileName, type, async, paramMap);
            Res101921 res = new Res101921();
            res.setObj(obj);
            log.addOperateLog("101921", JSON.toJSONString(req.getContent()), "EXCEL导入", "操作成功",
                LogOperateEnum.LOG_IMPORT.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            // 返回数据
            log.debug(req, "importExcel  success  end ");
            return Result.success(req, res);
        } catch (Exception e) {
            log.addOperateLog("101921", JSON.toJSONString(req.getContent()), "EXCEL导入失败", log.buildErrmsg(e),
                LogOperateEnum.LOG_IMPORT.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            // 调用微服务数据保存异常
            log.error(req, BizCode.SAVE_ERROR.getMessage(), e);
            return Result.error(req, BizCode.SAVE_ERROR.getCode(),
                StringUtil.isEmpty(e.getMessage()) ? BizCode.SAVE_ERROR.getMessage() : e.getMessage());
        }
    }

    @RequestMapping(value = "/101922", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取execl导入模板", notes = "获取execl导入模板")
    public Result<Res101922> getImportExcelTemplate(@RequestBody GatewayRequest<Req101922> req) {
        log.debug(req, "getImportExcelTemplate  {},param:{}  ", "begin", JSON.toJSONString(req.getContent()));
        Req101922 content = req.getContent();
        try {
            // 参数校验
            ValidationUtils.validateEntityStr(content);
        } catch (Exception e) {
            log.error(req, "getImportExcelTemplate  error  end ", e);
            return Result.error(req, BizCode.PARAM_ERROR.getCode(), e.getMessage());
        }
        try {
            String consumer = req.getContent().getConsumer();
            String type = req.getContent().getType();
            Res101922 res = new Res101922();
            String url = excelImportService.getExcelTemplateOfImport(consumer, type);
            if (StringUtil.isEmpty(url)) {
                log.debug(req, "importExcel  success  end ");
                return Result.error(req, "获取模板url为空");
            }
            res.setUrl(url);
            // 返回数据
            log.debug(req, "importExcel  success  end ");
            return Result.success(req, res);
        } catch (Exception e) {
            // 调用微服务数据保存异常
            log.error(req, BizCode.QUERY_ERROR.getMessage(), e);
            return Result.error(req, BizCode.QUERY_ERROR.getCode(),
                StringUtil.isEmpty(e.getMessage()) ? BizCode.QUERY_ERROR.getMessage() : e.getMessage());
        }
    }

    @RequestMapping(value = "/101923", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取导入任务列表", notes = "获取导入任务列表")
    public Result<Res101923> importTaskList(@RequestBody GatewayRequest<Req101923> req) {
        log.debug(req, "importTaskList  {},param:{}  ", "begin", JSON.toJSONString(req.getContent()));
        Req101923 content = req.getContent();
        try {
            // 参数校验
            ValidationUtils.validateEntityStr(content);
        } catch (Exception e) {
            log.error(req, "importTaskList  error  end ", e);
            return Result.error(req, BizCode.PARAM_ERROR.getCode(), e.getMessage());
        }
        try {
            Res101923 res = new Res101923();
            // 返回数据
            IPage<ImportTaskDto> list=excelImportService.importTaskList(content);
            res.setObj(list);
            log.debug(req, "importTaskList  success  end ");
            return Result.success(req, res);
        } catch (Exception e) {
            log.error(req, BizCode.QUERY_ERROR.getMessage(), e);
            return Result.error(req, BizCode.QUERY_ERROR.getCode(),
                    StringUtil.isEmpty(e.getMessage()) ? BizCode.QUERY_ERROR.getMessage() : e.getMessage());
        }
    }

}
